#!/usr/bin/python3.8.9
# -*- coding: utf-8 -*-

# @Author  : Tina Yu
# @Time    : 2021-8-17 10:56

import csv
from datetime import datetime

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

font_set = FontProperties(fname=r'c:\windows\fonts\simsun.ttc', size=14)


def get_weather_data(filename, dates, highs, lows):
    """从数据文件中获取最高温度和最低温度"""
    with open(filename) as f:
        reader = csv.reader(f)
        header_row = next(reader)

        #获取表头的索引
        date_index=header_row.index('DATE')
        high_index=header_row.index('TMAX')
        low_index=header_row.index('TMIN')

        # 从文件中获取日期、最高温度和最低温度
        for row in reader:
            current_date = datetime.strptime(row[date_index], '%Y-%m-%d')
            try:
                high = int(row[high_index])
                low = int(row[low_index])
            except ValueError:
                print(f"Missing data for {current_date}")
            else:
                dates.append(current_date)
                highs.append(high)
                lows.append(low)


# 获取sitka的温度数据
filename = 'sitka_weather_2018_simple.csv'
dates, highs, lows = [], [], []
get_weather_data(filename, dates, highs, lows)

# 将sitka数据绘制成图表
plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.plot(dates, highs, c='red', alpha=0.6)
ax.plot(dates, lows, c='blue', alpha=0.6)
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.15)

# 获取valley的温度数据
filename = 'death_valley_2018_simple.csv'
dates, highs, lows = [], [], []
get_weather_data(filename, dates, highs, lows)

# 将valley的数据添加到图表中
ax.plot(dates, highs, c='red', alpha=0.3)
ax.plot(dates, lows, c='blue', alpha=0.3)
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.05)

# 设置图表的格式
title = '对比图'
plt.title(title, fontproperties=font_set, fontsize=24)
plt.xlabel(r'日期', fontproperties=font_set)
fig.autofmt_xdate()
plt.ylabel('Temperature(F)', fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=8)
plt.ylim(10, 130)

plt.show()
